#!/bin/bash

# Ubuntu EC2 Quick Start Script for Alfresco 3.4/4.0
#
# Contributors: Will Abson, Mike Waldrop
# 
# This script will install a complete Alfresco system and all prerequisites on an Ubuntu Linux  
# host. The script has been tested on the official Oneiric image available from Canonical - 
# see https://help.ubuntu.com/community/EC2StartersGuide.
#
# TODO
#
# * Test JMX connections inbound
# * Support use of official Alfresco packages from partner repository
# * Allow packaging of sample content
#
# Ports required
#
# * 80 - HTTP
# * 25 and 143 for SMTP-in and IMAP (if enabled)
# * 8080 (admin purposes only)
# * 7070 - SharePoint protocol support
# * 50500 - JMX remote monitoring and control

# Begin configuration

# Download URL for MySQL Connector/J
CONNECTOR_DL_URL='http://mirrors.dedipower.com/www.mysql.com/Downloads/Connector-J/mysql-connector-java-5.1.18.tar.gz'
#CONNECTOR_DL_URL='http://mirrors.dedipower.com/www.mysql.com/Downloads/Connector-J/mysql-connector-java-5.1.10.tar.gz'
SWFTOOLS_DL_URL='http://www.swftools.org/swftools-0.9.1.tar.gz'

BASE_TEMP_DIR="/mnt"
ALF_TEMP_DIR="$BASE_TEMP_DIR/alfresco-temp"
#ALF_WAR_PKG="alfresco-war.tar.gz"
# WAR files now packaged as a ZIP file from 3.3E/3.4 Community onwards
ALF_WAR_PKG="alfresco-war.zip"
ALF_VTI_PKG="vti-module.zip"
CATALINA_BASE=/var/lib/tomcat6

# MySQL credentials for creating the Alfresco database
MYSQL_USER=root
MYSQL_PASSWORD=alfresco

# End configuration

# Do not edit below this line

# Base download location
dl_base="http://eu.dl.alfresco.com/release"

# Alfresco version to install
#alf_version=4.0.d

# Option for local install
local_install=0;

# Whether the Share webapp will be installed
alf_install_share=1

# Whether the mobile webapp will be installed
alf_install_mobile=0

# Whether SPP support will be installed
alf_install_vti=1

# Whether DOD modules will be installed
alf_install_dod=1

# Whether to enable IMAP
alf_enable_imap=0

# Whether to enable inbound SMTP - not yet implemented
alf_enable_smtp=0

# Whether or not to add bundled .sample files to shared/classes
alf_install_sample_config=0

# JDK to install
#jdk_version=sun-java6-jdk
jdk_version=default-jdk

# Whether to remove the downloaded WAR package after downloading
cleanup_war=1

# Whether to clean up the temp dir contents afterwards
cleanup_after=1

# Name of the script to run on start-up to set the public DNS name or IP address of the server
env_name=ec2

# Detect command-line options and their parameters
p_opt=""
for p in "$@"
do
  if [[ "$p" == --* ]]; then
    p_opt=""
    case "$p" in 
      --local-install)
        local_install=1;
        ;;
      --no-install-share)
        alf_install_share=0
        ;;
      --install-mobile)
        alf_install_mobile=1
        ;;
      --no-install-vti)
        alf_install_vti=0
        ;;
      --no-install-dod)
        alf_install_dod=0
        ;;
      --enable-imap)
        alf_enable_imap=1
        ;;
      --enable-smtp)
        alf_enable_smtp=1
        ;;
      --install-sample-config)
        alf_install_sample_config=1
        ;;
      --jdk-version)
        ;;
      --alf-edition)
        ;;
      --alf-version)
        ;;
      --alf-dl-url)
        ;;
      --alf-dl-base)
        ;;
      --env-name)
        ;;
      --no-cleanup-war)
        cleanup_war=0
        ;;
      --no-cleanup)
        cleanup_after=0
        cleanup_war=0
        ;;
      *)
        echo "Unrecognised option $p"
        exit 1
        ;;
    esac
    p_opt="$p"
  else
    case "$p_opt" in
      --jdk-version)
        jdk_version="$p"
        ;;
      --alf-version)
        alf_version="$p"
        alf_version_suffix="-$p"
        ;;
      --alf-edition)
        alf_edition="$p"
        ;;
      --alf-dl-url)
        alf_base_url="$p"
        ;;
      --alf-dl-base)
        dl_base="$p"
        ;;
      --env-name)
        env_name="$p"
        ;;
      *)
        echo "Unsupported parameter $p"
        exit 1
        ;;
    esac
    p_opt=""
  fi
done

function set_dl_params {
  test -z "$alf_base_url" && alf_base_url="$1"
  #test -z "$alf_edition" && alf_edition="$2"
  alf_edition="$2"
  test -z "$alf_version_suffix" && alf_version_suffix="-$alf_version"
}

# Alfresco Enterprise 4.0.2.0, 4.1.1, 4.1.1.3 - WA added 2012-11-09
# Alfresco Community 4.0.e, 4.2.a, 4.2.b - WA added 2012-11-09
# Alfresco Enterprise 4.0.2 - WA added 2012-06-26
# Alfresco Enterprise 4.0.1 - WA added 2012-05-01
# Alfresco Enterprise 4.0.0 - WA added 2012-02-10
# Alfresco Enterprise 4.0beta - WA added 2012-01-20
# Alfresco Community 4.0.d - WA added 2012-02-10
# Alfresco Community 4.0.c - WA added 2012-01-20
# Alfresco Community 4.0.b - WA added 2012-01-20
# Alfresco Community 4.0.a - WA added 2011-10-16
# Alfresco Community 3.4.d - WA added 2011-04-01
# Alfresco Community 3.4.c - WA added 2011-01-07
# Alfresco Community 3.4.b - WA added 2010-11-30
# Alfresco Community 3.4.a - WA added 2010-11-30

case "$alf_version" in
  4.1.1.3)
    set_dl_params "$dl_base/enterprise/4.1/build-00061" "enterprise"
    ;;
  4.1.1)
    set_dl_params "$dl_base/enterprise/4.1/build-159" "enterprise"
    ;;
  4.0.2.9)
    set_dl_params "$dl_base/enterprise/4.0/build-00038" "enterprise"
    ;;
  4.0.2)
    set_dl_params "$dl_base/enterprise/4.0/build-966" "enterprise"
    ;;
  4.0.1)
    set_dl_params "$dl_base/enterprise/4.0/build-897" "enterprise"
    ;;
  4.0.0)
    set_dl_params "$dl_base/enterprise/4.0/build-800" "enterprise"
    ;;
  4.0.0beta)
    set_dl_params "$dl_base/enterprise/4.0/build-643" "enterprise"
    ;;
  4.2.b)
    set_dl_params "$dl_base/community/build-04480" "community"
    ;;
  4.2.a)
    set_dl_params "$dl_base/community/build-4428" "community"
    ;;
  4.0.e)
    set_dl_params "$dl_base/community/build-00007" "community"
    ;;
  4.0.d)
    set_dl_params "$dl_base/community/build-4003" "community"
    ;;
  4.0.c)
    set_dl_params "$dl_base/community/build-3979" "community"
    ;;
  4.0.b)
    set_dl_params "$dl_base/community/build-3835" "community"
    ;;
  4.0.a)
    set_dl_params "$dl_base/community/build-3755" "community"
    ;;
  3.4.d)
    set_dl_params "$dl_base/community/build-3370" "community"
    ;;
  3.4.c)
    set_dl_params "$dl_base/community/build-3335" "community"
    ;;
  3.4.b)
    set_dl_params "$dl_base/community/build-3262" "community"
    ;;
  3.4.a)
    set_dl_params "$dl_base/community/build-3169" "community"
    ;;
  3.2r2)
    set_dl_params "$dl_base/community/build-2440" "community"
    alf_dod_module_suffix="-unsupported"
    ;;
  *)
    echo "Unsupported version $alf_version"
    exit 1
    ;;
esac

# Old download URLs (unsupported)

# Alfresco Community 3.2r2 downloads
#ALF_DL_URL='http://dl.alfresco.com/release/community/build-2440/alfresco-community-war-3.2r2.tar.gz'
#ALF_MMT_URL='http://dl.alfresco.com/release/community/build-2440/alfresco-mmt-3.2r2.jar'
#ALF_VTI_MODULE_URL='http://dl.alfresco.com/release/community/build-2440/vti-module.amp'
#ALF_DOD_MODULE_URL='http://dl.alfresco.com/release/community/build-2440/alfresco-dod5015-unsupported.amp'
#ALF_DOD_SHARE_MODULE_URL='http://dl.alfresco.com/release/community/build-2440/alfresco-dod5015-share-unsupported.amp'

#ALF_DL_URL="$alf_base_url/alfresco-enterprise-war$alf_version_suffix.tar.gz"

# Construct individual download URLs
case "$alf_version_suffix" in
  *-3.4*)
    ALF_DL_URL="$alf_base_url/alfresco-$alf_edition$alf_version_suffix.zip"
    ALF_VTI_MODULE_URL="$alf_base_url/alfresco-$alf_edition-spp$alf_version_suffix.zip"
    ALF_DOD_MODULE_URL="$alf_base_url/alfresco-$alf_edition-dod5015$alf_version_suffix.zip"
    ;;
  *-3.*)
    ALF_DL_URL="$alf_base_url/alfresco-enterprise-war$alf_version_suffix.zip"
    ALF_MMT_URL="$alf_base_url/alfresco-mmt$alf_version_suffix.jar"
    ALF_VTI_MODULE_URL="$alf_base_url/vti-module.amp"
    # DOD files now have version number in the file name for 3.3e
    ALF_DOD_MODULE_URL="$alf_base_url/alfresco-dod5015$alf_version_suffix.amp"
    ALF_DOD_SHARE_MODULE_URL="$alf_base_url/alfresco-dod5015-share$alf_version_suffix.amp"
    ;;
  *)
    ALF_DL_URL="$alf_base_url/alfresco-$alf_edition$alf_version_suffix.zip"
    ALF_VTI_MODULE_URL="$alf_base_url/alfresco-$alf_edition-spp$alf_version_suffix.zip"
    ALF_DOD_MODULE_URL="$alf_base_url/alfresco-$alf_edition-dod5015$alf_version_suffix.zip"
    ;;
esac

# Name of the script to run on start-up to set the public DNS name or IP address of the server
local_init_script="$env_name-init.sh"

# Name of the motd script to run on start-up
local_motd_script="$env_name-update-motd.sh"

# APT sources vars
apt_repo_url=`grep '^deb ' /etc/apt/sources.list | head -1 | cut -d ' ' -f 2`
apt_repo_name=`grep '^deb ' /etc/apt/sources.list | head -1 | cut -d ' ' -f 3`
ubuntu_release=$apt_repo_name

function dl_package {
  echo "Downloading '$1' via HTTP"
  curl --dump-header headers.txt -o $2 $1
  httpstatus=`cat headers.txt | head -1| cut -d ' ' -f 2`
  if [ "$httpstatus" != "200" ]; then
    echo "Error: Got HTTP status $httpstatus"
    exit 1
  fi
  rm headers.txt
}

# Copy WAR file from an exploded Alfresco ZIP package
function copy_war {
  if [ -f "$1/$2" ]; then
    cp -p "$1/$2" $3
  else
    if [ -f "$1/web-server/webapps/$2" ]; then
      cp -p "$1/web-server/webapps/$2" $3
    else
      echo "Could not find webapp '$2' in '$1'"
    fi
  fi
}

function unpack_war {
  war_file="$1/$2"
  war_dir="$1"/`echo $2 | cut -d . -f 1`
  if [ ! -d "$war_dir" ]; then
    mkdir "$war_dir"
    unzip -qq "$war_file" -d "$war_dir"
    chown -R tomcat6:tomcat6 "$war_dir"
  fi
}

function pack_war {
  war_file="$1/$2"
  war_dir="$1"/`echo $2 | cut -d . -f 1`
  cd "$war_dir"
  chmod -R a+r .
  zip -uq "$war_file" "*"
  chown -R tomcat6:tomcat6 "$war_file"
  cd - >/dev/null
}

function install_amp {
  if [ ! -f /var/lib/alfresco/alfresco-mmt.jar ]; then
    echo "Module Management Tool not found!"
    exit 1
  fi
  java -jar /var/lib/alfresco/alfresco-mmt.jar install "$2" "$1"
  # Make sure new webapp has read permissions for all
  chmod +r "$1"
}

function apt_add_source {
  if [ -z "`grep "^$1" /etc/apt/sources.list`" ]; then
    echo "$1" >> /etc/apt/sources.list
  fi
}

function set_property {
  if [ -f "$1" ]; then
    esckey=${2//\./\\.}
    existing=`grep "^#*\s*$esckey=.*" "$1"`
    if [ -n "$existing" ]; then
      escval=${3//\//\\\/}
      escval=${escval//\./\\.}
      sed -i "s/#*\s*$esckey=.*/$esckey=$escval/" "$1"
    else
      echo "$2=$3" >> "$1"
    fi
  else
    echo "$2=$3" >> "$1"
  fi
}

function build_swftools {
  # swftools compilation - not required for karmic or lucid as v0.9.0 exists in the repository
  wget "$SWFTOOLS_DL_URL"
  swftools=`find . -name "swftools*" -type f`
  if [ ! -z "$swftools" ]; then
    tar xzf "$swftools"
    swftoolsdir=`find . -name "swftools*" -type d`
    if [ ! -z "$swftoolsdir" ]; then
      cd "$swftoolsdir"
      apt-get --yes install build-essential zlib1g-dev libfreetype6-dev libjpeg-dev libungif4-dev
      ./configure && make
      checkinstall --pkgname=swftools --pkgversion "4:0.9.1-1" --backup=no --deldoc=yes --fstrans=no --default
      cd ..
    fi
    #rm -rf swftools-*
  else
    echo "Could not locate SWFTools download file"
    exit 1
  fi
}

function install_bootstrap_data {
  mkdir "$2"
  unzip "$1" -d "$2"
  cp "alfresco-bootstrap-data.sh" "/etc/init.d/alfresco-bootstrap-data"
  update-rc.d "alfresco-bootstrap-data" defaults 98 02
}

# Install the Module Management Tool from the WAR package. If it is not found
# then download it instead.
function install_mmt_tool {
  MMT_JAR="/var/lib/alfresco/alfresco-mmt.jar"
  if [ ! -f "$MMT_JAR" ]; then 
    if [ -f "$ALF_TEMP_DIR/commands/bin/alfresco-mmt.jar" ]; then
      cp "$ALF_TEMP_DIR/commands/bin/alfresco-mmt.jar" "$MMT_JAR"
    else
      if [ -f "$ALF_TEMP_DIR/bin/alfresco-mmt.jar" ]; then
        cp "$ALF_TEMP_DIR/bin/alfresco-mmt.jar" "$MMT_JAR"
      else
        echo "Downloading Module Management Tool"
        dl_package "$ALF_MMT_URL" "$MMT_JAR"
      fi
    fi
    chown tomcat6:tomcat6 "/var/lib/alfresco/alfresco-mmt.jar"
  fi
}

# Copy sample config files from the WAR package
function copy_extension_config {
  # Pre 3.3 WAR package
  if [ -d "$1/extensions/extension" ]; then
    mkdir "$2/shared/classes/alfresco"
    cp -pr "$1/extensions/extension" "$2/shared/classes/alfresco"
  else
    # 3.3 WAR (include alfresco-global.properties)
    if  [ -d "$1/extensions/alfresco" ]; then
      cp -pr "$1/extensions/alfresco"* "$2/shared/classes"
    else
      # 3.4 WAR
      if [ -d "$1/web-server/shared/classes" ]; then
        cp -pr "$1/web-server/shared/classes/alfresco"* "$2/shared/classes"
      else
        echo "Could not find extensions in '$1'"
      fi
    fi
  fi
  set_ownership $2/shared/classes/alfresco*
}

function set_ownership {
  chown -R tomcat6:tomcat6 $1
}

# Check user if root
if [ "$(whoami)" != "root" ]; then
   echo "This script requires root permissions to run"
   exit 1
fi

if [ $local_install = "1" ]; then
	if [ -f "$ALF_WAR_PKG" ]; then
		echo "Doing local install of $ALF_WAR_PKG"
	else
		echo "Local install file $ALF_WAR_PKG missing"
		exit 1
	fi
	if [ -f "$ALF_VTI_PKG" ]; then
		echo "Doing local install of $ALF_VTI_PKG"
	else
		echo "Local install file $ALF_VTI_PKG missing"
		exit 1
	fi
fi

#
# Install dependencies
#

echo "Updating APT repositories"
# Need to add multiverse for JDK
apt_add_source "deb $apt_repo_url $apt_repo_name multiverse"
apt_add_source "deb-src $apt_repo_url $apt_repo_name multiverse"
apt_add_source "deb $apt_repo_url $apt_repo_name-updates multiverse"
apt_add_source "deb-src $apt_repo_url $apt_repo_name-updates multiverse"
apt-get -qq update

# Sun JDK needs the user to manually agree to the DLJ license
case $jdk_version in
  sun-java6-jdk)
    echo "Installing Sun JDK 1.6"
    apt-get --yes -qq install sun-java6-jdk
    java_home="/usr/lib/jvm/java-6-sun/"
    ;;
  sun-java5-jdk)
    echo "Installing Sun JDK 1.5"
    apt-get --yes -qq install sun-java5-jdk
    java_home="/usr/lib/jvm/java-1.5.0-sun/"
    ;;
  openjdk-6)
    echo "Installing OpenJDK 1.6"
    apt-get --yes -qq install openjdk-6-jdk
    java_home="/usr/lib/jvm/java-6-openjdk"
    ;;
  openjdk-7)
    echo "Installing OpenJDK 1.7"
    apt-get --yes -qq install openjdk-7-jdk
    java_home="/usr/lib/jvm/java-7-openjdk-i386"
    ;;
  default-jdk)
    echo "Installing default JDK"
    apt-get --yes -qq install default-jdk
    java_home="/usr/lib/jvm/default-java"
    ;;
  *)
    echo "Unsupported JDK $jdk_version"
    exit 1
    ;;
esac

# MySQL server will prompt for a root password - set this to 'alfresco'
echo "Installing MySQL Server"
apt-get --yes -qq install mysql-server

echo "Installing Apache Tomcat"
apt-get --yes -qq install tomcat6

echo "Installing Apache HTTPd"
apt-get --yes -qq install apache2

echo "Installing OpenOffice.org"
apt-get --yes -qq install openoffice.org
# Only needed on systems prior to Karmic
apt-get --yes -qq install openoffice.org-headless

echo "Installing additional tools"
apt-get --yes -qq install imagemagick
apt-get --yes -qq install zip unzip
apt-get --yes -qq install ec2-ami-tools
apt-get --yes -qq install checkinstall

# swftools should not be installed from APT for Ubuntu versions older than Karmic as a newer version is required
if [ -z `which pdf2swf` ]; then
  apt-get --yes -qq install swftools || build_swftools
fi

# Stop Tomcat ready for changes to webapp files and config
echo "Stopping Tomcat ready for main installation"
/etc/init.d/tomcat6 stop

# Download WAR package
echo "Downloading Alfresco WAR package as $ALF_WAR_PKG"
if [ ! -f "$ALF_WAR_PKG" ]; then
  echo "Downloading $ALF_DL_URL"
  dl_package "$ALF_DL_URL" "$ALF_WAR_PKG"
else
  echo "Using local $ALF_WAR_PKG"
fi

  # Extract Alfresco files
  echo "Extracting downloaded files"
  if [ ! -d "$ALF_TEMP_DIR" ]; then mkdir "$ALF_TEMP_DIR"; fi
  case "$ALF_WAR_PKG" in
    *.zip)
      unzip -q "$ALF_WAR_PKG" -d "$ALF_TEMP_DIR"
      ;;
    *.tar.gz)
      tar -xz -f "$ALF_WAR_PKG" -C "$ALF_TEMP_DIR"
      ;;
    *)
      echo "Archive not a supported format: $ALF_WAR_PKG"
      exit 1
  esac
  if [ $cleanup_war -eq 1 ]; then rm "$ALF_WAR_PKG"; fi

# Create directory structures for Alfresco
if [ ! -d /var/lib/alfresco ]; then mkdir /var/lib/alfresco; fi
if [ ! -d /var/log/alfresco ]; then mkdir /var/log/alfresco; fi
chown tomcat6:tomcat6 /var/log/alfresco/
chown tomcat6:tomcat6 /var/lib/alfresco/

# Install MMT
install_mmt_tool

# Copy WAR files into place and extract if necessary
echo "Copying WAR files into place and unpacking"
if [ ! -f $CATALINA_BASE/webapps/alfresco.war ]; then
  copy_war "$ALF_TEMP_DIR" "alfresco.war" "$CATALINA_BASE/webapps/"
fi
if [ "$alf_install_share" -eq 1 -a ! -f $CATALINA_BASE/webapps/share.war ]; then
  copy_war "$ALF_TEMP_DIR" "share.war" "$CATALINA_BASE/webapps/"
fi
if [ "$alf_install_mobile" -eq 1 -a ! -f $CATALINA_BASE/webapps/mobile.war ]; then
  copy_war "$ALF_TEMP_DIR" "mobile.war" "$CATALINA_BASE/webapps/"
fi

# Unpack the WAR files so we can make required log4j config changes
if [ ! -d $CATALINA_BASE/webapps/alfresco ]; then
  unpack_war $CATALINA_BASE/webapps alfresco.war
fi
if [ ! -d $CATALINA_BASE/webapps/share ]; then
  unpack_war $CATALINA_BASE/webapps share.war
fi

# Create and enable use of shared/classes and shared/lib in Tomcat config
if [ ! -d $CATALINA_BASE/shared/classes/alfresco ]; then mkdir -p $CATALINA_BASE/shared/classes/alfresco; fi
if [ ! -d $CATALINA_BASE/shared/lib ]; then mkdir -p $CATALINA_BASE/shared/lib; fi
set_property /etc/tomcat6/catalina.properties 'shared.loader' '${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar'
if [ ! -L /usr/share/tomcat6/shared ]; then ln -s /var/lib/tomcat6/shared /usr/share/tomcat6/shared; fi
set_ownership $CATALINA_BASE/shared/

# Copy extensions into shared/classes folder
if [ $alf_install_sample_config -a ! -d $CATALINA_BASE/shared/classes/alfresco/extension ]; then
  copy_extension_config "$ALF_TEMP_DIR" "$CATALINA_BASE"
fi

echo "Configuring Alfresco"

f=$CATALINA_BASE/shared/classes/alfresco-global.properties

# Edit alfresco-global.properties
set_property "$f" "hostname.public" '${localname}'
set_property "$f" "dir.root" "/var/lib/alfresco/alf_data"
set_property "$f" "ooo.exe" "soffice"
set_property "$f" "ooo.user" "/home/tomcat6"
set_property "$f" "img.root" "/usr/bin"
set_property "$f" "img.exe" "convert"
# Set location of pdf2swf explicitly since tomcat6 user does not have /usr/local/bin on the path
set_property "$f" "swf.exe" `which pdf2swf`

# Set Database properties
set_property "$f"  "db.driver" "org.gjt.mm.mysql.Driver"
set_property "$f"  "db.url" "jdbc:mysql://localhost:3306/alfresco"
set_property "$f"  "db.username" "alfresco"
set_property "$f"  "db.password" "alfresco"

# Set outbound SMTP properties - requires local SMTP server e.g. Postfix
set_property "$f"  "mail.host" "localhost"
set_property "$f"  "mail.username" ""

# Enable IMAP
if [ "$alf_enable_imap" == "1" ]; then
  set_property "$f" "imap.server.enabled" "true"
  set_property "$f" "imap.server.host" "0.0.0.0"
fi

# set CIFS and FTP properties
set_property "$f" "cifs.enabled" "false"
set_property "$f" "ftp.enabled" "true"
set_property "$f" "ftp.port" "2121"

# Set host name and port
set_property "$f" "alfresco.host" '${hostname.public}'
set_property "$f" "alfresco.port" "80"
set_property "$f" "share.host" '${hostname.public}'
set_property "$f" "share.port" "80"

# Web client and Share proxy support
for cf in `find config -name "*$alf_version_suffix.xml"`; do
  origname=`echo $cf | sed s/$alf_version_suffix// | sed s/config\\\\///`
  cp $cf $CATALINA_BASE/shared/classes/$origname
  chown -R tomcat6:tomcat6 $CATALINA_BASE/shared/classes/alfresco
done

# Change alfresco.log file location in log4j.properties
set_property "$CATALINA_BASE/webapps/alfresco/WEB-INF/classes/log4j.properties" "log4j.appender.File.File" "/var/log/alfresco/alfresco.log"
if [ "$alf_install_share" == "1" ]; then
  set_property "$CATALINA_BASE/webapps/share/WEB-INF/classes/log4j.properties" "log4j.appender.File.File" "/var/log/alfresco/share.log"
fi
if [ "$alf_install_mobile" == "1" ]; then
  set_property "$CATALINA_BASE/webapps/mobile/WEB-INF/classes/log4j.properties" "log4j.appender.File.File" "/var/log/alfresco/mobile.log"
fi
# Add updated files back to the WAR
pack_war $CATALINA_BASE/webapps alfresco.war
if [ "$alf_install_share" == "1" ]; then
  pack_war $CATALINA_BASE/webapps share.war
fi

# Install VTI module
if [ "$alf_install_vti" == "1" ]; then
  listvti="$( java -jar /var/lib/alfresco/alfresco-mmt.jar list $CATALINA_BASE/webapps/alfresco.war | grep org.alfresco.module.vti )"
  if [ -z "$listvti" ]; then
    echo "Installing VTI module"
    if [ "$local_install" == "1" ]; then
      echo "Using local VTI package vti-module.zip"
      unzip -q $ALF_VTI_PKG "*.amp"
      install_amp $CATALINA_BASE/webapps/alfresco.war alfresco-$alf_edition-spp$alf_version_suffix.amp
      rm alfresco-$alf_edition-spp$alf_version_suffix.amp $ALF_VTI_PKG
    else
      case "$ALF_VTI_MODULE_URL" in
        *.amp)
          dl_package "$ALF_VTI_MODULE_URL" vti-module.amp
          install_amp $CATALINA_BASE/webapps/alfresco.war vti-module.amp
          rm vti-module.amp
        ;;
        *.zip)
          dl_package "$ALF_VTI_MODULE_URL" vti-module.zip
          unzip -q vti-module.zip "*.amp"
          install_amp $CATALINA_BASE/webapps/alfresco.war alfresco-$alf_edition-spp$alf_version_suffix.amp
          rm alfresco-$alf_edition-spp$alf_version_suffix.amp vti-module.zip
        ;;
      esac
	fi
    set_property "$f" "vti.alfresco.alfresoHostWithPort" 'http://${hostname.public}'
    set_property "$f" "vti.share.shareHostWithPort" 'http://${hostname.public}'
    set_property "$f" "vti.server.external.host" '${hostname.public}'
  fi
fi

# Install DOD modules
if [ "$alf_install_dod" == "1" ]; then
  echo "Installing DOD modules"
  listdod="$( java -jar /var/lib/alfresco/alfresco-mmt.jar list $CATALINA_BASE/webapps/alfresco.war | grep org_alfresco_module_dod5015 )"
  if [ -z "$listdod" -a -n "$ALF_DOD_MODULE_URL" ]; then
    case "$ALF_DOD_MODULE_URL" in
      *.amp)
        dl_package "$ALF_DOD_MODULE_URL" alfresco-dod5015.amp
        install_amp $CATALINA_BASE/webapps/alfresco.war alfresco-dod5015.amp
        rm alfresco-dod5015.amp
      ;;
      *.zip)
        dl_package "$ALF_DOD_MODULE_URL" alfresco-dod5015.zip
        unzip -q alfresco-dod5015.zip "*.amp"
        install_amp $CATALINA_BASE/webapps/alfresco.war alfresco-$alf_edition-dod5015$alf_version_suffix.amp
        rm alfresco-$alf_edition-dod5015$alf_version_suffix.amp
      ;;
    esac
  fi
  listdodshare="$( java -jar /var/lib/alfresco/alfresco-mmt.jar list $CATALINA_BASE/webapps/share.war | grep org_alfresco_module_dod5015_share )"
  if [ -z "$listdodshare" -a "$alf_install_share" == "1" ]; then
    case "$ALF_DOD_MODULE_URL" in
      *.amp)
        dl_package "$ALF_DOD_SHARE_MODULE_URL" alfresco-dod5015-share.amp
        install_amp $CATALINA_BASE/webapps/share.war alfresco-dod5015-share.amp
        rm alfresco-dod5015-share.amp
      ;;
      *.zip)
        if [ ! -f alfresco-dod5015.zip ]; then dl_package "$ALF_DOD_MODULE_URL" alfresco-dod5015.zip; fi
        if [ ! -f alfresco-$alf_edition-dod5015-share$alf_version_suffix.amp ]; then unzip -q alfresco-dod5015.zip "*.amp"; fi
        install_amp $CATALINA_BASE/webapps/share.war alfresco-$alf_edition-dod5015-share$alf_version_suffix.amp
        rm alfresco-$alf_edition-dod5015-share$alf_version_suffix.amp
      ;;
    esac
  fi
  if [ -f alfresco-dod5015.zip ]; then rm alfresco-dod5015.zip; fi
fi

# Append marker for user data properties to alfresco-global.properties
echo "# START USER DATA" >> "$f"

# Remove web-app folders to force deployment of new apps
rm -rf $CATALINA_BASE/webapps/alfresco
if [ -d $CATALINA_BASE/webapps/share ]; then
  rm -rf $CATALINA_BASE/webapps/share
fi

# Download and install Connector/J
jarfiles=`find "$CATALINA_BASE" -name "mysql-connector-java-*-bin.jar"`
if [ -z "$jarfiles" ]; then
  echo "Downloading and installing MySQL Connector/J"
  curl $CONNECTOR_DL_URL -o $BASE_TEMP_DIR/mysql-connector-java.tar.gz
  jar=`tar tzf $BASE_TEMP_DIR/mysql-connector-java.tar.gz | grep 'mysql-connector-java.*\-bin.jar'`
  tar xzf $BASE_TEMP_DIR/mysql-connector-java.tar.gz -C $BASE_TEMP_DIR $jar
  mv $BASE_TEMP_DIR/$jar $CATALINA_BASE/shared/lib/
  chown tomcat6:tomcat6 $CATALINA_BASE/shared/lib/*.jar
  rm -rf $BASE_TEMP_DIR/mysql-connector*
fi

# Set Tomcat Java defaults
# Use Sun JDK not OpenJDK
# Disable Tomcat security policies
echo "Configuring Tomcat"
set_property /etc/default/tomcat6 "JAVA_HOME" "\"$java_home\""
set_property /etc/default/tomcat6 "JAVA_OPTS" "\"-Xms128m -Xmx1024m -Xss128k -XX:MaxPermSize=160m -Dalfresco.home=/var/lib/alfresco -Dcom.sun.management.jmxremote\""
set_property /etc/default/tomcat6 "TOMCAT6_SECURITY" "no"

# Karmic Tomcat6 package links to eclipse-ecj.jar for jasper-jdt.jar, which breaks MyFaces
if [ "$ubuntu_release" == "karmic" -a -L "/usr/share/tomcat6/lib/jasper-jdt.jar" ]; then
  unlink "/usr/share/tomcat6/lib/jasper-jdt.jar"
  cp "jasper-jdt.jar" "/usr/share/tomcat6/lib"
fi

# Enable AJP Connector if it is commented out
ln=`grep -n '\s*<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />\s*' /etc/tomcat6/server.xml | cut -d ":" -f 1`
lc="$( wc -l /etc/tomcat6/server.xml | cut -d " " -f 1 )"
if [[ "`head -n $((ln-1)) /etc/tomcat6/server.xml | tail -n 1`" == *\<\!--* ]]; then
  if [[ "`head -n $((ln+1)) /etc/tomcat6/server.xml | tail -n 1`" == *--\>* ]]; then
    head -n $((ln-2)) /etc/tomcat6/server.xml > /etc/tomcat6/server.xml.head
    tail -n $((lc-ln-1)) /etc/tomcat6/server.xml > /etc/tomcat6/server.xml.tail
    tail -n $((lc-ln+1)) /etc/tomcat6/server.xml | head -n 1 > /etc/tomcat6/server.xml.this
    cat /etc/tomcat6/server.xml.head /etc/tomcat6/server.xml.this /etc/tomcat6/server.xml.tail > /etc/tomcat6/server.xml
    rm /etc/tomcat6/server.xml.head /etc/tomcat6/server.xml.this /etc/tomcat6/server.xml.tail
  fi
fi

# Configure the tomcat6 user - a home directory and a proper shell are needed to run OpenOffice
echo "Configuring tomcat6 user"
if [ ! -d /home/tomcat6 ]; then mkdir /home/tomcat6; fi
chown tomcat6:tomcat6 /home/tomcat6
usermod --home /home/tomcat6 --shell /bin/bash tomcat6

# Compiling swftools (no longer required on Karmic)
# Configure MySQL and restart
if [ ! -f /etc/mysql/conf.d/alfresco.cnf ]; then
  echo "Configuring MySQL server"
  cp alfresco.cnf /etc/mysql/conf.d/alfresco.cnf
  echo "Restarting MySQL"
  service mysql restart
fi

# Create the 'alfresco' database and grant privileges
echo "Creating MySQL database"
cat create-database.sql | mysql -u $MYSQL_USER -p$MYSQL_PASSWORD

# Edit apache configuration
echo "Configuring Apache HTTPd"
if [ ! -f /etc/apache2/conf.d/alfresco ]; then
  cp alfresco-apache2.conf /etc/apache2/conf.d/alfresco
fi
if [ -f /etc/apache2/mods-available/proxy.conf ]; then ln -s /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf; fi
if [ -f /etc/apache2/mods-available/proxy_ajp.load ]; then ln -s /etc/apache2/mods-available/proxy_ajp.load /etc/apache2/mods-enabled/proxy_ajp.load; fi
if [ -f /etc/apache2/mods-available/proxy.load ]; then ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load; fi
if [ ! -f /var/www/custom-503.html ]; then cp custom-503.html /var/www; fi
echo "Restarting HTTPd"
/etc/init.d/apache2 restart

# Tuning
echo "Final system configuration"
# Copy init scripts which updates IP addresses, etc. - runs on every startup
cp "$local_init_script" /var/lib/alfresco
if [ -z "`grep "$local_init_script" /etc/rc.local`" ]; then
  sed -i.orig "s/^exit 0\$/bash \\/var\\/lib\\/alfresco\\/$local_init_script\\nexit 0/" /etc/rc.local 
fi
cp "$local_motd_script" "/etc/update-motd.d/60-alfresco"
# Check the ulimit is set up properly
if [ "`grep \"tomcat6\\s*nofile\" /etc/security/limits.conf`" == "" ]; then
  sed -i.orig 's/^# End of file.*$/tomcat6\tsoft\tnofile\t4096\ntomcat6\thard\tnofile\t65536\n\n# End of file/' /etc/security/limits.conf
fi

# Tidy up
echo "Cleaning up"
if [ -d "$ALF_TEMP_DIR" -a $cleanup_after -eq 1 ]; then
  rm -rf $ALF_TEMP_DIR
fi

echo "Alfresco installed successfully"
exit

# End

# Start Tomcat
#/etc/init.d/tomcat6 start

# Wiping the repo
#rm -rf /var/lib/alfresco/alf_data/
#cat drop-database.sql | mysql -u root -palfresco

